//
//  JiuDianViewController.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/20.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "JiuDianViewController.h"
#import "DetailJiuDianViewController.h"
#import "HBHBannerViewController.h"
#import <SDCycleScrollView.h>

#import "HBHChouseTypeView.h"
#import "HeaderChooseView.h"
#import "CCSDSortView.h"
#import "CCSDChouseKindView.h"
#import "HBHChouseAreaView.h"

#import "scrollVCell.h"
#import "HotelDetailCell.h"
#import "JiuDianCell.h"
#import "HBHMainIconCell.h"
#import "TLCityPickerController.h"

@interface JiuDianViewController ()<HWDNavHeadViewDelegate,UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate,BMKLocationServiceDelegate,BMKRouteSearchDelegate,TLCityPickerDelegate>

@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) BaseBottomView *chooseV;
@property (nonatomic, strong) HBHChouseTypeView *oldHBView;
@property (nonatomic, strong) HeaderChooseView *headerView;
@property (nonatomic, strong) CCSDSortView *allSortView;
@property (nonatomic, strong) CCSDSortView *togetherSortView;
@property (nonatomic, strong) CCSDSortView *brandSortView;

@property (nonatomic) NSInteger pageNo;
@property (nonatomic, retain) NSMutableArray *btnArr;
@property (nonatomic, retain) NSArray *titles;//搜索标题
@property (nonatomic, retain) NSMutableArray *hotels;
@property (nonatomic, retain) NSDictionary *selectKind;
@property (nonatomic, retain) NSArray *orderBys;
@property (nonatomic, retain) NSDictionary *orderBy;
@property (nonatomic, retain) NSDictionary *tradingArea;
@property (nonatomic, retain) NSDictionary *area;
@property (nonatomic) BOOL isChouseType;
@property (nonatomic, strong) NSMutableArray *lunBoArr;//轮播数组

/**
 定位类
 */
@property (nonatomic, strong) BMKLocationService *locService;

/**
 用户位置
 */
@property (nonatomic, strong) BMKUserLocation *userLocation;

@end

@implementation JiuDianViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self creatView];
    
    [self creatRightButton];
    
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    if ([_typeDic[@"type"] integerValue] == 1) {
        [self getClassificationBeanList];
    }
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    [self.locService startUserLocationService];
    [self addLunBoRequest];
}

-(void)creatView
{
    _pageNo = 1;
    _titles = @[@"全部",@"附近",@"智能排序"];
    _hotels = [NSMutableArray new];
    _isChouseType = NO;
    _selectKind = @{@"name":@"全部"};
    _orderBys = @[@{@"name":@"智能排序",@"id":@""},@{@"name":@"离我最近",@"id":@"distance asc"},@{@"name":@"好评优先",@"id":@"star_class desc"},@{@"name":@"人气最高",@"id":@"consume_number desc"}];
    _orderBy = @{@"name":@"智能排序",@"id":@""};
    _btnArr = [NSMutableArray new];
    _lunBoArr = [NSMutableArray new];
    
//    [self initSearchBarWithPlaceholder:@"请输入关键字搜索" showsCancelButton:NO];
    [self initTitleWithTitle:_typeDic[@"name"]];
//    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//    btn.frame = CGRectMake(0, 0, 20*screenW_6, 15*screenW_6);
//    [btn addTarget:self action:@selector(onTapLeftBarButton) forControlEvents:UIControlEventTouchUpInside];
//    [btn addSubview:[TomamaUI createImageView:CGRectMake(0, 0, 9*screenW_6, 15*screenW_6) backGroundImageV:@"back"]];
//    UIBarButtonItem *barButtonitem = [[UIBarButtonItem alloc] initWithCustomView:btn];
//    self.navigationItem.leftBarButtonItem = barButtonitem;
    
    [self creatTableView];
    [self.view addSubview:self.table];
}

-(void)onTapLeftBarButton
{
    if (self.JiuDianBlock) {
        self.JiuDianBlock(_cityName, _cityID);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)creatRightButton
{
    UILabel *cityLabel = [TomamaUI createLabel:CGRectMake(0, 0, [TomamaUI getStringWeithtWith:_cityName andHeigth:17*screenW_6 andFont:[UIFont systemFontOfSize:17*screenW_6]], 17*screenW_6) textAligment:1 font:[UIFont systemFontOfSize:17*screenW_6] textColor:TextWhite text:_cityName];
    if (cityLabel.frame.size.width > [TomamaUI getStringWeithtWith:_cityName andHeigth:17*screenW_6 andFont:[UIFont systemFontOfSize:17*screenW_6]]) {
        cityLabel.frame = CGRectMake(0, 0, [TomamaUI getStringWeithtWith:_cityName andHeigth:17*screenW_6 andFont:[UIFont systemFontOfSize:17*screenW_6]], 17*screenW_6);
    }
    cityLabel.center = CGPointMake(cityLabel.center.x , 22*screenW_6);
    
    UIView *view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(cityLabel.frame), 44*screenW_6)];
    [view addSubview:cityLabel];
    [view addSubview:cityLabel];
    
    UITapGestureRecognizer *righttap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(rightTap)];
    [view addGestureRecognizer:righttap];
    
    UIBarButtonItem *rightButtonitem = [[UIBarButtonItem alloc] initWithCustomView:view];
    self.navigationItem.rightBarButtonItem = rightButtonitem;
}

-(CCSDSortView *)allSortView
{
    if (!_allSortView) {
        _allSortView = [[CCSDSortView alloc]initWithFrame:CGRectMake(0, 0, 115*screenW_6, 40*screenW_6) title:@"全部"];
        __weak typeof(self) weakSelf = self;
        _allSortView.chouseComplete = ^(){
            weakSelf.isChouseType = YES;
            [weakSelf.table reloadData];
            UIView *view1 = [weakSelf.view viewWithTag:101];
            UIView *view2 = [weakSelf.view viewWithTag:102];
            [view1 removeFromSuperview];
            [view2 removeFromSuperview];
            weakSelf.togetherSortView.isSelect = NO;
            [weakSelf.togetherSortView creatViewWithTitle:weakSelf.tradingArea[@"name"]?weakSelf.tradingArea[@"name"]:@"附近"];
            weakSelf.brandSortView.isSelect = NO;
            [weakSelf.brandSortView creatViewWithTitle:weakSelf.orderBy[@"name"]];
            CCSDChouseKindView *view = [[CCSDChouseKindView alloc]initDatas:weakSelf.btnArr selectData:weakSelf.selectKind];
            view.complete = ^(NSDictionary *kind) {
                weakSelf.selectKind = kind;
                [weakSelf changeSort];
            };
            view.dismissComplete = ^(){
                weakSelf.allSortView.isSelect = NO;
                [weakSelf.allSortView creatViewWithTitle:weakSelf.selectKind[@"name"]];
                weakSelf.isChouseType = NO;
                [weakSelf.table reloadData];
            };
            [view showOnView:weakSelf.view];
            view.tag = 100;
        };
        
        _allSortView.unChouseComplete = ^(){
            UIView *view = [weakSelf.view viewWithTag:100];
            [view removeFromSuperview];
            weakSelf.isChouseType = NO;
            [weakSelf.table reloadData];
        };
    }
    return _allSortView;
}

-(CCSDSortView *)togetherSortView
{
    if (!_togetherSortView) {
        _togetherSortView = [[CCSDSortView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.allSortView.frame), 0, SCREENW - 230*screenW_6, 40*screenW_6) title:@"附近"];
        __weak typeof(self) weakSelf = self;
        _togetherSortView.chouseComplete = ^(){
            weakSelf.isChouseType = YES;
            [weakSelf.table reloadData];
            UIView *view1 = [weakSelf.view viewWithTag:100];
            UIView *view2 = [weakSelf.view viewWithTag:102];
            [view1 removeFromSuperview];
            [view2 removeFromSuperview];
            weakSelf.allSortView.isSelect = NO;
            [weakSelf.allSortView creatViewWithTitle:weakSelf.selectKind[@"name"]?weakSelf.selectKind[@"name"]:@"全部"];
            weakSelf.brandSortView.isSelect = NO;
            [weakSelf.brandSortView creatViewWithTitle:weakSelf.orderBy[@"name"]];
            
            HBHChouseAreaView *view = [[HBHChouseAreaView alloc]initUserLocation:weakSelf.userLocation selectArea:weakSelf.area selectTradingArea:weakSelf.tradingArea complete:^(NSDictionary *area, NSDictionary *tradingArea) {
                weakSelf.isChouseType = YES;
                [weakSelf.table reloadData];
                weakSelf.area = area;
                weakSelf.tradingArea = tradingArea;
                [weakSelf changeSort]; 
            }];
            [view showOnView:weakSelf.view];
            
            view.disMissBlock = ^(){
                weakSelf.togetherSortView.isSelect = NO;
                [weakSelf.togetherSortView creatViewWithTitle:weakSelf.tradingArea[@"name"]?weakSelf.tradingArea[@"name"]:@"附近"];
                weakSelf.isChouseType = NO;
                [weakSelf.table reloadData];
            };
            [view showOnView:weakSelf.view];
            view.tag = 101;
        };
        
        _togetherSortView.unChouseComplete = ^(){
            UIView *view = [weakSelf.view viewWithTag:101];
            [view removeFromSuperview];
            weakSelf.isChouseType = NO;
            [weakSelf.table reloadData];
        };
    }
    return _togetherSortView;
}

-(CCSDSortView *)brandSortView
{
    if (!_brandSortView) {
        _brandSortView = [[CCSDSortView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.togetherSortView.frame), 0, 115*screenW_6, 40*screenW_6) title:_orderBy[@"name"]];
        __weak typeof(self) weakSelf = self;
        _brandSortView.chouseComplete = ^(){
            weakSelf.isChouseType = YES;
            [weakSelf.table reloadData];
            UIView *view1 = [weakSelf.view viewWithTag:101];
            UIView *view2 = [weakSelf.view viewWithTag:100];
            [view1 removeFromSuperview];
            [view2 removeFromSuperview];
            weakSelf.allSortView.isSelect = NO;
            [weakSelf.allSortView creatViewWithTitle:weakSelf.selectKind[@"name"]?weakSelf.selectKind[@"name"]:@"全部"];
            weakSelf.togetherSortView.isSelect = NO;
            [weakSelf.togetherSortView creatViewWithTitle:weakSelf.tradingArea[@"name"]?weakSelf.tradingArea[@"name"]:@"附近"];
            
            CCSDChouseKindView *view = [[CCSDChouseKindView alloc]initDatas:weakSelf.orderBys selectData:weakSelf.orderBy];
            view.complete = ^(NSDictionary *kind) {
                weakSelf.orderBy = kind;
                [weakSelf changeSort];
            };
            view.dismissComplete = ^(){
                weakSelf.brandSortView.isSelect = NO;
                [weakSelf.brandSortView creatViewWithTitle:weakSelf.orderBy[@"name"]];
                weakSelf.isChouseType = NO;
                [weakSelf.table reloadData];
            };
            [view showOnView:weakSelf.view];
            view.tag = 102;
        };
        
        _brandSortView.unChouseComplete = ^(){
            UIView *view = [weakSelf.view viewWithTag:102];
            [view removeFromSuperview];
            weakSelf.isChouseType = NO;
            [weakSelf.table reloadData];
        };
    }
    return _brandSortView;
}

- (void)creatTableView
{
    _table = [[UITableView alloc]initWithFrame:CGRectMake(0,0, SCREENW, SCREENH-64) style:UITableViewStylePlain];
    _table.delegate = self;
    _table.dataSource = self;
    _table.showsHorizontalScrollIndicator = NO;
    _table.showsVerticalScrollIndicator = NO;
    _table.separatorStyle = 0;
    _table.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    _table.backgroundColor = [UIColor clearColor];
    
    _table.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        _pageNo = 1;
        [_hotels removeAllObjects];
        [self addShangJiaRequest];
    }];
    //加载
    _table.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        _pageNo++;
        [self addShangJiaRequest];
    }];
}

//-(UITableView *)table
//{
//    if (!_table) {
//        _table = [[UITableView alloc]initWithFrame:CGRectMake(0,0, SCREENW, SCREENH-64) style:UITableViewStylePlain];
//        _table.delegate = self;
//        _table.dataSource = self;
//        _table.showsHorizontalScrollIndicator = NO;
//        _table.showsVerticalScrollIndicator = NO;
//        _table.separatorStyle = 0;
//        _table.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
//        _table.backgroundColor = [UIColor clearColor];
//
//        _table.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
//            _pageNo = 1;
//            [_hotels removeAllObjects];
//            [self addShangJiaRequest];
//        }];
//        //加载
//        _table.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
//            _pageNo++;
//            [self addShangJiaRequest];
//        }];
//    }
//    return _table;
//}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (_isChouseType) {
        return 1;
    }
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (_isChouseType) {
        return 40*screenW_6;
    }
    if (section != 0) {
        return 40*screenW_6;
    }
    return 0.000001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (_isChouseType) {
        return .1*screenW_6;
    }
    if (section == 1) {
        return .1;
    }
    return 5*screenW_6;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_isChouseType) {
        return _hotels.count;
    }
    if (section == 0) {
        if ([_typeDic[@"type"] integerValue] == 2) {
            return 1;
        }
        return 2;
    }
    return _hotels.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    __weak typeof(self) weakSelf = self;
    if (indexPath.section == 0 && !_isChouseType) {
        if (indexPath.row == 0) {
            scrollVCell *cell= [tableView dequeueReusableCellWithIdentifier:@"scrollVCell"];
            if (!cell) {
                cell = [[scrollVCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"scrollVCell"];
            }
            NSMutableArray *images = [NSMutableArray new];
            for (int i = 0; i<[_lunBoArr count]; i++) {
                [images addObject:_lunBoArr[i][@"image"]];
            }
            [cell loadCellWithData:images];
            
            cell.block = ^(NSInteger index){
//                HBHBannerViewController *vc = [HBHBannerViewController new];
//                vc.bannerId = weakSelf.lunBoArr[index][@"id"];
//                [weakSelf.navigationController pushViewController:vc animated:YES];
            };
            
            return cell;
        }else{//主按钮Cell
            HBHMainIconCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HBHMainIconCell"];
            if (!cell) {
                cell = [[HBHMainIconCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HBHMainIconCell"];
            }
            
            if (_typeThree == 1) {

                [cell loadCellWithThreeIcons:self.btnArr];

            }else{
                [cell loadCellWithIcons:self.btnArr];
            }
            
            cell.chooseMainBtnBlock = ^(NSInteger index){
            
                JiuDianViewController *vc = [JiuDianViewController new];
                vc.typeDic = weakSelf.btnArr[index + 1];
                vc.cityName = _cityName;
                vc.cityID = _cityID;
                [weakSelf.navigationController pushViewController:vc animated:YES];
                vc.JiuDianBlock = ^(NSString *cityName, NSString *cityID) {
                    _cityName = cityName;
                    _cityID = cityID;
                    [_hotels removeAllObjects];
                    [self creatRightButton];
                    [self addLunBoRequest];
                    [self addShangJiaRequest];
                };
            };
            return cell;
        }
    }else{//酒店列表
        JiuDianCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JiuDianCell"];
        if (!cell) {
            cell = [[JiuDianCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"JiuDianCell"];
        }
        [cell loadCellWithData:_hotels[indexPath.row]];
        return cell;
    }
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1 || _isChouseType) {//跳转酒店详情
        if (indexPath.row >= _hotels.count) {
            return;
        }
        if (![Storage isLogin]) {
            [Storage goToLogin];
        }else{
            DetailJiuDianViewController *vc = [DetailJiuDianViewController new];
            vc.hotelId = _hotels[indexPath.row][@"id"];
            vc.userLocation = _userLocation;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 1 || _isChouseType) {
//        __weak typeof(self) weakSelf = self;
        UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 40*screenW_6)];
        topView.backgroundColor = TextWhite;
        [self.view addSubview:topView];
        [topView addSubview:self.allSortView];
        [topView addSubview:self.togetherSortView];
        [topView addSubview:self.brandSortView];
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 40*screenW_6 - 1, SCREENW, 1)];
        lineView.backgroundColor = backColor;
        [topView addSubview:lineView];
        
        for (int i = 0; i < 2; i++) {
            UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(114.5*screenW_6 + (SCREENW - 230*screenW_6)*i, 0, 1, 14*screenW_6)];
            lineView.backgroundColor = [UIColor colorWithHexString:@"#e5e5e5"];
            [topView addSubview:lineView];
            lineView.center = CGPointMake(lineView.center.x, topView.frame.size.height/2);
        }
        return topView;
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && !_isChouseType) {
        if (indexPath.row == 0) {
            return 180*screenW_6;
        }else{
            if (_btnArr.count == 1 || _btnArr.count == 0) {
                return 0;
            }
            NSInteger num = (_btnArr.count - 1)/4 + ((_btnArr.count - 1)%4?1:0);
            return 68*num*screenW_6 + 15*screenW_6;
        }
    }else{
        return 150*screenW_6;
    }
    return 0;
}

#pragma mark -
-(void)onTapRightBarButton
{
    
}

-(void)changeSort
{
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    _pageNo = 1;
    [_hotels removeAllObjects];
    [_table.mj_footer resetNoMoreData];
    [self addShangJiaRequest];
}

#pragma mark - request
-(void)getClassificationBeanList
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/other/classificationBeanList" Dic:@{@"parentId":_typeDic[@"id"],@"type":@"2"} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
//                NSLog(@"res:%@",responseDic);
                if ([responseDic[@"success"] boolValue]) {
                    _btnArr = [NSMutableArray arrayWithArray:responseDic[@"data"]];
                    [_btnArr insertObject:@{@"name":@"全部"} atIndex:0];
                    [_table reloadData];
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"success"] timeInterval:2];
                }
            });
        }];
    });
}

-(void)addLunBoRequest//轮播请求
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/other/classificationBannerList" Dic:
         @{@"kindId":_typeDic[@"id"],@"area.id":_cityID} completion:^(NSDictionary *responseDic) {
               dispatch_async(dispatch_get_main_queue(), ^{
                   if ([responseDic[@"success"] boolValue]) {
                       _lunBoArr = [NSMutableArray arrayWithArray:responseDic[@"data"]];
                       //刷新第一行
                       [_table reloadData];
                   }
                   if ([_table.mj_header isRefreshing]) {
                       [_table.mj_header endRefreshing];
                   }
               });
           }];
    });
}

-(void)addShangJiaRequest{//商家请求
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSString *classification = _typeDic[@"id"];
        if (![_selectKind[@"name"] isEqualToString:@"全部"]) {
            classification = _selectKind[@"id"];
        }
        NSString *tradingAreaId = @"";
        if (_tradingArea) {
            tradingAreaId = _tradingArea[@"id"];
        }
        [HttpRequest PostrequestUrl:@"/business/businessBeanList" Dic:@{@"classification.id":classification,@"longitude":[NSString stringWithFormat:@"%lf",_userLocation.location.coordinate.longitude],@"latitude":[NSString stringWithFormat:@"%lf",_userLocation.location.coordinate.latitude],@"orderBy":_orderBy[@"id"],@"pageNo":[NSString stringWithFormat:@"%ld",_pageNo],@"pageSize":@"10",@"tradingArea.id":tradingAreaId,@"city.id":_cityID} completion:^(NSDictionary *responseDic) {
           dispatch_async(dispatch_get_main_queue(), ^{
               if ([responseDic[@"success"] boolValue]) {
                   if (_hotels.count < [responseDic[@"count"] integerValue]) {
                       [_hotels addObjectsFromArray:responseDic[@"data"]];
                   }
               }else{
                   [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
               }
               [_table reloadData];
               [HudHelper hideHudToView:self.view];
               if ([_table.mj_header isRefreshing]) {
                   [_table.mj_header endRefreshing];
               }
               if ([_table.mj_footer isRefreshing]) {
                   if (_hotels.count >= [responseDic[@"count"] integerValue]) {
                       [_table.mj_footer endRefreshingWithNoMoreData];
                   }else{
                       [_table.mj_footer endRefreshing];
                   }
               }
           });
        }];
    });
}

-(void)rightTap//省市区选择
{
    TLCityPickerController *cityPickerVC = [[TLCityPickerController alloc] init];
    [cityPickerVC setDelegate:self];
    cityPickerVC.hotCitys = @[@"武汉市", @"襄阳市", @"黄冈市"];//热门城市
    [self presentViewController:[[UINavigationController alloc] initWithRootViewController:cityPickerVC] animated:YES completion:^{
    }];
}
#pragma mark - TLCityPickerDelegate
- (void) cityPickerController:(TLCityPickerController *)cityPickerViewController didSelectCity:(NSString *)city didSelectCityID:(NSString *)ID
{
    //城市选择的跳转 选择的回调
    [cityPickerViewController dismissViewControllerAnimated:YES completion:^{
        _cityName = city;
        _cityID = ID;
        _pageNo = 1;
        [_hotels removeAllObjects];
        [self creatRightButton];
        [self addLunBoRequest];
        [self addShangJiaRequest];
        [standfault setObject:city forKey:@"cityName"];
        [standfault setObject:ID forKey:@"cityID"];
        [standfault synchronize];
    }];
}

- (void) cityPickerControllerDidCancel:(TLCityPickerController *)cityPickerViewController
{
    //取消的回调
    [cityPickerViewController dismissViewControllerAnimated:YES completion:^{
    }];
}

#pragma mark - BMKLocationService
-(BMKLocationService *)locService
{
    if (!_locService) {
        _locService = [[BMKLocationService alloc]init];
        _locService.delegate = self;
    }
    return _locService;
}

/**
 *用户位置更新后，会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    _userLocation = userLocation;
    if (_userLocation) {
        [self.locService stopUserLocationService];
        [self addShangJiaRequest];
        [HudHelper hideHudToView:self.view];
    }
}

/**
 *定位失败后，会调用此函数
 *@param error 错误号，参考CLError.h中定义的错误号
 */
- (void)didFailToLocateUserWithError:(NSError *)error
{
    [HudHelper hideHudToView:self.view];
}

@end
